package com.it2297.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.it2297.main.App;
import com.it2297.main.UserApp;
import com.it2297.view.WorkshopView;

public class WorkshopParticipant {
	private int participantId;
	private int workshopId;
	private String title;
	private String name;
	private String nric;
	
	public int getParticipantId() {
		return participantId;
	}
	public void setParticipantId(int participantId) {
		this.participantId = participantId;
	}
	public int getWorkshopId() {
		return workshopId;
	}
	public void setWorkshopId(int workshopId) {
		this.workshopId = workshopId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNric() {
		return nric;
	}
	public void setNric(String nric) {
		this.nric = nric;
	}
	private void setAttributes(ResultSet resultSet) {
		try {
			this.setParticipantId(resultSet.getInt("participant_id"));
			this.setWorkshopId(resultSet.getInt("workshop_id"));
			this.setTitle(resultSet.getString("title"));
			this.setName(resultSet.getString("name"));
			this.setNric(resultSet.getString("nric"));
					
			
			
		} catch (SQLException ex) {
			ex.printStackTrace();
		}

	}
	
	
	
	
	
	public WorkshopParticipant getSingle(int id) {
		try {
			App.mySqlConnection.open();

			ResultSet resultSet = App.mySqlConnection.executeQuery("SELECT * FROM americano.workshop_participant where participant_id = " +id+"");

			

			if (resultSet.next()) {
				this.setAttributes(resultSet);
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			App.mySqlConnection.close();
		}
		
		return this;
	}
	public boolean createWorkshopParticipant(WorkshopParticipant workshopparticipant){
		boolean success = false;
		
		int participantId=workshopparticipant.getParticipantId();
		int workshopId=workshopparticipant.getWorkshopId();
		String title=workshopparticipant.getTitle();
		String name=workshopparticipant.getName();
		String nric=workshopparticipant.getNric();
		
		
		try{
			App.mySqlConnection.open();
			
			String sql = "INSERT INTO americano.workshop_participant(participant_id, workshop_id, title, name, nric)";
			sql+="VALUES ('" +participantId+"','"+workshopId+ "','"+title+ "','"+name+"','"+nric+"')";
			App.mySqlConnection.executeUpdate(sql);
				success = true;
			
		}
		catch(Exception ex){
			ex.printStackTrace();
			
		}
		finally{
			App.mySqlConnection.close();
		}
		return success;
	}
	public static boolean IsWorkshopIdExists(int id){
		boolean result = false;
		ResultSet rs = null;
		String sql = "SELECT * FROM americano.workshop_participant WHERE participant_id='" + id + "'";
		
		try {
			App.mySqlConnection.open();
			rs = App.mySqlConnection.executeQuery(sql);
			if (rs.next())
				result = true;
			else
				result = false;
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			App.mySqlConnection.close();
		}
		return result;
	}
	public static ArrayList<WorkshopParticipant> getMultiple() {
		ArrayList<WorkshopParticipant> workshops = new ArrayList<WorkshopParticipant>();

		try {
			App.mySqlConnection.open();

			
			ResultSet resultSet = App.mySqlConnection.executeQuery("SELECT * FROM americano.workshop_participant where nric='"+UserApp.getCurrentUser().getNric()+"'" );

			while (resultSet.next()) {
				WorkshopParticipant workshop=new WorkshopParticipant();
				workshop.setAttributes(resultSet);
				workshops.add(workshop);
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			App.mySqlConnection.close();
		}

		return workshops;
	}
	public  boolean delWorkshopJoined(WorkshopParticipant workshoppart) {
		boolean success = false;
       
		int id = workshoppart.getParticipantId();
		
		try {
			App.mySqlConnection.open();
			String sql = "DELETE FROM americano.workshop_participant WHERE participant_id = '" + id + "'";
			
			
			App.mySqlConnection.executeUpdate(sql);
			success = true;
			
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		finally{
			App.mySqlConnection.close();
		}
		return success;
	}
	
	
	

}
